<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/5/23
 * Time: 15:21
 */

namespace app\api\controller;

use EasyWeChat\Factory;
use app\backend\model\SysSite as SysSiteModel;
use EasyWeChat\Kernel\BaseClient;



class ZhLiveBroadcast extends Common
{
    static private $mediaUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=";
    
    static private $createRoomUrl = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=";
    
    static private $editRoomUrl = "https://api.weixin.qq.com/wxaapi/broadcast/room/editroom?access_token=";

    static private $getLiveUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=";

    
    static private $accessToken;
    
    static private $sendData;
    
    public function uploadMedia($data){
        //        $fileData = ['media' => "@".$imgDir];
        $res = http_post(self::$mediaUrl,$data);
        return $res;
    }
    
    /**
     * 创建直播间
     * @param $data
     * @return bool|string
     */
    public function createRoom($data){
        self::$createRoomUrl = self::$createRoomUrl.self::$accessToken;
        self::$sendData = [
            'name'        => $data['live_name'],
            'coverImg'    => $data['live_cover_media_id'],
            'startTime'   => strtotime($data['live_date'].' '.$data['live_start_time']),
            'endTime'     => strtotime($data['live_date'].' '.$data['live_end_time']),
            'anchorName'  => $data['live_anchor_name'],
            'anchorWechat'=> $data['live_anchor_wechat'],
            'shareImg'    => $data['live_share_media_id'],
            'feedsImg'    => $data['live_feeds_media_id'],
            'type'        => $data['type'],
            'closeLike'   => $data['live_close_like'],
            'closeGoods'  => $data['live_close_goods'],
            'closeComment'=> $data['live_close_comment'],
            'closeReplay' => $data['live_close_replay'],
            'closeShare'  => $data['live_close_share'],
            'closeKf'     => $data['live_close_kf'],
        ];
        $res = http_post(self::$createRoomUrl,http_build_query(self::$sendData));
        $res = json_decode($res,true);
        $res['data'] = $data;
        $res['sendData'] = self::$sendData;
        return $res;
    }
    
    
    /**获取直播间**/
    public function getLiveLists(){
        $wxapp_id     = $this->request->get('wxapp_id');
        $wxSetting    = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        $config = [
            'app_id'=> $wxSetting['app_id'],
            'secret'=> $wxSetting['app_secret']
        ];
        $app = Factory::officialAccount($config);
        // 获取 access token 实例
        $accessToken = $app->access_token;
        $token = $accessToken->getToken(); // 强制重新从微信服务器获取 token.
        self::$accessToken = $token['access_token'];
        self::$getLiveUrl = self::$getLiveUrl . self::$accessToken;
        $limit  = $this->request->get('limit', 20, 'intval');
        $page   = $this->request->get('page', 0, 'intval');
        
        self::$sendData = [
            'start'=> $page,
            'limit'=> $limit,
        ];
        self::$sendData = json_encode(self::$sendData);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::$getLiveUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, self::$sendData);
        $data = json_decode(curl_exec($ch),true);
        return $this->ajaxReturn($this->successCode,'成功',$data);
        curl_close($ch);
    }
}